// 引入express
const express = require('express')
const indexRouter = express.Router() // 路由对象

const list = [
	{
		username: 'lisi',
		password: 123,
	},
]

/**
 * 登录
 */
indexRouter.get('/login', (request, response) => {
	const username = request.query.username
	const password = request.query.password
	const user = list.find(item => item.username == username && item.password == password)
	if (user) {
		response.send({
            code:1,
            message:'登录成功'
        })
	} else {
		response.send({
            code:-1,
            message:'登录失败'
        })
	}
})

/**
 * 注册
 */
indexRouter.get('/reg', (request, response) => {
	const username = request.query.username
	const password = request.query.password
    // 判断用户是否已经注册过
	const user = list.find(item => item.username === username)
	if (user) {
		response.send({
            code:-1,
            message:'用户已经注册'
        })
	} else {
		list.push({
			username: username,
			password: password,
		})
		response.send({
            code:1,
            message:'注册成功'
        })
	}
})

module.exports = {indexRouter}
